package com.wwwarehouse.common.custom_widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.wwwarehouse.common.R;

public class ArcProgressView extends View {

	Context mContext = null;
	Paint mPaintCircle = null;
	Paint mPaintHu = null;
	float drawDegree = 0;
	public ArcProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public ArcProgressView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context c) {
		mContext = c;
		mPaintCircle = new Paint();
		mPaintCircle.setStyle(Paint.Style.STROKE);
		mPaintCircle.setColor(mContext.getResources().getColor(R.color.white));
		mPaintCircle.setStrokeWidth(dip2px(mContext, 4));
		mPaintCircle.setAntiAlias(true);

		mPaintHu = new Paint();
		mPaintHu.setStyle(Paint.Style.STROKE);
		mPaintHu.setColor(mContext.getResources().getColor(R.color.blue));
		mPaintHu.setStrokeWidth(dip2px(mContext, 4));
		mPaintHu.setAntiAlias(true);
	}

	int progress = 60; // 进度
	int startAngle = -90; // 开始角度

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int x = getWidth() / 2;
		int y = getHeight() / 2;
		int radius = getWidth() / 2 - 10;

		// 画圆环
		canvas.drawCircle(x, y, radius, mPaintCircle);
		// 画圆弧
		RectF rect = new RectF(getWidth()/2 - radius, 10, getWidth()/2 + radius, getHeight() - 10);
		canvas.drawArc(rect, startAngle, drawDegree, false, mPaintHu);
	}
	//设置弧的颜色
	public void setHuColor(int color){
		mPaintHu.setColor(color);
	}
	float count;
	public void setData(float count) {
		this.count = count;
		if (count >= 85) {
			mPaintHu.setColor(mContext.getResources().getColor(R.color.blue));
		}
		else {
			mPaintHu.setColor(mContext.getResources().getColor(R.color.red));
		}

		drawDegree = count / 100 * 360;
		ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, drawDegree);
		valueAnimator.setDuration(1000);
		// 匀速线性插值器
		valueAnimator.setInterpolator(new LinearInterpolator());
		valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				// 当插值计算进行时，获取中间的每个值，
				// 这里这个值是中间过程中的曲线长度（下面根据这个值来得出中间点的坐标值）
				drawDegree = (Float) animation.getAnimatedValue();
				invalidate();
			}
		});
		// 开始动画
		valueAnimator.start();
	}

	public int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}
